package com.cph.es.service;

import com.cph.es.pojo.VehicleCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AddIndexTypeService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    public Map<String, Object> createIndexType() {
        Map<String, Object> indexMap = new HashMap<>();
        try {
            indexMap = elasticsearchTemplate.getMapping(VehicleCar.class);
        } catch (Exception e) {
            elasticsearchTemplate.createIndex(VehicleCar.class);
            elasticsearchTemplate.putMapping(VehicleCar.class);
            indexMap = elasticsearchTemplate.getMapping(VehicleCar.class);
        }

        return indexMap;
    }

    public Boolean deleteIndexType() {
        Boolean ifDelete = elasticsearchTemplate.deleteIndex(VehicleCar.class);
        return ifDelete;
    }
}
